package me.shurufa.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.google.gson.Gson;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.io.File;

import me.shurufa.MainApplication;
import me.shurufa.model.User;

/**
 * 全局静态变量
 *
 * @FileName: me.shurufa.utils.Global.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-14 12:25
 */
public class Global {
  public static boolean DEBUG = false;
  // about device
  public static String appVersionName;
  public static String appVersionCode;
  public static String osVersion;
  public static String imei;
  public static String DeviceUniqueID;
  // about user
  public static User currentUser;
  // about cache with json
  public static File cacheJsonDir;
  // about third party
  // wechat
  public static IWXAPI mWxApi;
  // baidu
  public static String BAIDU_USER_ID;
  public static String BAIDU_CHANNEL_ID;
  // Global Gson object
  private static final ThreadLocal<Gson> reuseGson = new ThreadLocal<Gson>() {
    @Override
    protected Gson initialValue() {
      return new Gson();
    }
  };


  /**
   * 初始化数据调用
   * 
   * @param context
   */
  public static void appInit(Context context) {
    // baidupush
//未设置    PushManager.startWork(context, PushConstants.LOGIN_TYPE_API_KEY, Constants.BAIDU_KEY);
    // wechat init
//未设置    mWxApi = WXAPIFactory.createWXAPI(context, Constants.WECHAT_KEY);
//未设置    mWxApi.registerApp(Constants.WECHAT_KEY);
    // unique
    if (Global.DeviceUniqueID == null) {
      Global.DeviceUniqueID =
          android.provider.Settings.Secure.getString(context.getContentResolver(),
              android.provider.Settings.Secure.ANDROID_ID);
      if (Global.DeviceUniqueID == null) {
        Global.DeviceUniqueID = String.valueOf(System.currentTimeMillis() % 6000);
      }
    }
    // cachedir sp for current user
    File path = context.getExternalCacheDir();
    cacheJsonDir = new File(path, "json");
    cacheJsonDir.mkdirs();

    currentUser = Settings.getCurrentUser();
    // mobile info -------start
    try {
      PackageManager manager = context.getPackageManager();
      PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
      appVersionName = info.versionName;
      appVersionCode = String.valueOf(info.versionCode);
    } catch (Exception e) {
      e.printStackTrace();
    }
    osVersion = "Android_" + Build.VERSION.SDK_INT;
    imei = Utils.getIMEI(MainApplication.getInstance());
    // ---------end
  }

  /**
   * 获取Gson对象
   * 
   * @return
   */
  public static Gson getGson() {
    return reuseGson.get();
  }

  /**
   * 获取当前用户Uid
   * 
   * @return
   */
  public static long getCurrentUid() {
    if (currentUser != null)
      return currentUser.uid;
    return 0L;
  }

  /**
   * 判断客户端是否登录
   * 
   * @return
   */
  public static boolean isLoggedIn() {
    return getCurrentUid() != 0L;
  }

  /**
   * 获取当前时间
   * 
   * @return
   */
  public static long getCurrentTime() {
    return System.currentTimeMillis();
  }

  /**
   * 全局context
   * 
   * @return
   */
  public static Context getContext() {
    return MainApplication.getInstance();
  }
}
